﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


using PowerCreator.LMS.Web.Question;
using PowerCreator.LMS.Question.Entiry;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Common.Script;

public partial class sysadmin_QuestionManager_EditQuestionInfo_panduan : PowerCreator.LMS.Web.Base.BasePage
{
    //ManagerCourseInfo courseInfoManager = new ManagerCourseInfo();
    //ManagerQuestionTypeInfo questionTypeManager = new ManagerQuestionTypeInfo();

    //ManagerQuestionInfo questionInfoManager = new ManagerQuestionInfo();
    ManagerQuestion objQuestion = new ManagerQuestion();
    Course objCourse = new Course();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["TypeID"] = Request.QueryString["TypeID"].ToString();
            ViewState["QuestionID"] = Request.QueryString["qid"].ToString();
            Pageload();
            InitPage();
        }
    }
    protected override void InitPage()
    {
        lab_ContentTitle.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_ContentTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_QuestionCount.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionContent", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_QuestionOption.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionOption", PowerCreator.LMS.Language.LanagetItemType.Page);
        

        btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnBack.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);

       
        base.InitPage();
    }
    private void Pageload()
    {
        VOQuestion objQuestionInfo = new VOQuestion();
        objQuestionInfo = objQuestion.LoadQuestion(int.Parse(ViewState["QuestionID"].ToString()));
        ViewState["objQuestionInfo"] = objQuestionInfo;

        txtQuestionContent.Text = objQuestionInfo.QuestionContent;


        #region 绑定选项
        ListItem objlist;
        for (int i = 0; i < 2; i++)
        {
            objlist = new ListItem();
            switch (i)
            {
                case 0: objlist.Text = "A";
                    //objlist.Selected = true;
                    break;
                case 1: objlist.Text = "B";
                    break;

            }
            objlist.Value = i.ToString();

   
        }

        #endregion

        #region 绑定选项内容
        DataTable dt_QuestionOption = new DataTable();
        string search = "FKQuestionOptionQuestionID=" + ViewState["QuestionID"].ToString();
        dt_QuestionOption = objQuestion.QueryQuestionOption(search);
        ViewState["dt_QuestionOption"] = dt_QuestionOption;

        for (int i = 0; i < dt_QuestionOption.Rows.Count; i++)
        {
            string Content = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString();
            string Option=Content.Substring(0, 1);
            if (Option == "A")
            {
                txtOptionA.Text = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString().Remove(0, 2);
            }
            else
            {
                txtOptionB.Text = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString().Remove(0, 2);
            }

        }
        #endregion

    }
    private bool PageCheck()
    {

        return true;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!PageCheck())
        {
            return;
        }
        VOQuestion objQuestionInfo = new VOQuestion();
        objQuestionInfo = (VOQuestion)ViewState["objQuestionInfo"]; 
        objQuestionInfo.FKQuestionCourseID = -1;
        //objQuestionInfo.QuestionCreateID = int.Parse(Session["UserID"].ToString());
        //info.DateCreated = DateTime.Now;

        objQuestionInfo.QuestionAnswer = "";
        objQuestionInfo.QuestionDefaultScore = 0;
        objQuestionInfo.QuestionDiffculty =0;
        objQuestionInfo.QuestionKen ="";


        objQuestionInfo.QuestionContent = txtQuestionContent.Text;


        objQuestionInfo.QuestionState = 2;         //禁用1,启用2


        if (string.IsNullOrEmpty(txtOptionA.Text.Trim()))       //临时使用这种方式，有很多问题，在外面修改时需要注意，至少应该有四项选择
        {
            Script.Alert(Page, "msg", GetLanageValueByKey("AddPaper_msg14", PowerCreator.LMS.Language.LanagetItemType.Page) + "A" + GetLanageValueByKey("AddPaper_msg15", PowerCreator.LMS.Language.LanagetItemType.Page));
            return;
        }

        if (string.IsNullOrEmpty(txtOptionB.Text.Trim()))
        {
            Script.Alert(Page, "msg", GetLanageValueByKey("AddPaper_msg14", PowerCreator.LMS.Language.LanagetItemType.Page) + "B" + GetLanageValueByKey("AddPaper_msg15", PowerCreator.LMS.Language.LanagetItemType.Page));
            return;
        }

        VOQuestionOption objQuestionOptionInfo = new VOQuestionOption();
        DataTable dt_QuestionOption =(DataTable) ViewState["dt_QuestionOption"];
        for (int i = 0; i < dt_QuestionOption.Rows.Count; i++)
        {
            string Content = dt_QuestionOption.Rows[i]["QuestionOptionContent"].ToString();
            string Option = Content.Substring(0, 1);
            string optionID = dt_QuestionOption.Rows[i]["QuestionOptionID"].ToString();
            objQuestionOptionInfo = objQuestion.LoadQuestionOpgion(int.Parse(optionID));
            if (Option == "A")
            {
                
                objQuestionOptionInfo.QuestionOptionContent ="A、" +txtOptionA.Text;
            }
            else
            {
                objQuestionOptionInfo.QuestionOptionContent = "B、" + txtOptionB.Text;
            }
            objQuestion.UpdateQuetionOption(objQuestionOptionInfo);

        }
        if (objQuestion.UpdateQuetion(objQuestionInfo))//更新试题信息
        {
            Script.Alert(this, "", GetLanageValueByKey("msg_CelueSelectQuest_shuruti23", PowerCreator.LMS.Language.LanagetItemType.Page));
        }
        else
        {
            Script.Alert(this, "", GetLanageValueByKey("msg_CelueSelectQuest_shuruti24", PowerCreator.LMS.Language.LanagetItemType.Page));
        }

        Script.RedirectURL("ManagerQuestionInfo.aspx");
  
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Script.RedirectURL("ManagerQuestionInfo.aspx");
    }
}
